package com.helsinki.mapper;

import com.helsinki.pojo.Role;
import com.helsinki.pojo.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface RoleMapper {
    @Select("select * from role where id in " +
            "(select roleId from users_role where userId = #{uid})")
    @Results(value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "roleName", property = "roleName"),
            @Result(column = "roleDesc", property = "roleDesc"),
            @Result(property = "permissions", column = "id",
                    many = @Many(select = "com.helsinki.mapper.PermissionMapper.findByRid"))
    })
    List<Role> findByUid(String uid);

    @Select("select * from role where id in " +
            "(select roleId from users_role where userId = #{uid})")
    List<Role> findOtherRolesByUid(String uid);

    @Select("select userId from users_role where roleId = #{rid}")
    List<String> findUserByRId(String rid);

    @Select("select * from Role")
    List<Role> findAll();

    @Insert("insert into role (id,roleName, roleDesc) " +
            "values(#{id},#{roleName},#{roleDesc})")
    void addRole(Role role);

    @Update("update role set " +
            "roleName = #{roleName}," +
            "roleDesc = #{roleDesc} " +
            "where id = #{id}")
    void updateById(Role role);

    @Select("select * from role where id = #{id}")
    Role queryById(String id);

    @Delete("delete from role_permission where roleId = #{id}")
    void deletePermissionByRid(String id);

    @Insert("insert into role_permission (permissionId, roleId)" +
            "values(#{permissionId},#{roleId})")
    void insertPermissionByRid(@Param("permissionId") String permissionId,
                               @Param("roleId") String roleId);

    @Delete("delete from role where Id = #{id}")
    void deleteById(String id);

    //设置外键约束
    @Update("SET foreign_key_checks = 0")
    void closeForeignKey();

    @Update("SET foreign_key_checks = 1")
    void openForeignKey();


}